package me.zhengjie.modules.finace_taxation.rest;

import com.alibaba.fastjson.JSONObject;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.finace_taxation.domain.FtIndustryDetails;
import me.zhengjie.modules.finace_taxation.domain.FtLiveBroadcast;
import me.zhengjie.modules.finace_taxation.domain.FtLiveBroadcastImport;
import me.zhengjie.modules.finace_taxation.service.FtLiveBroadcastService;
import me.zhengjie.modules.finace_taxation.service.dto.FtLiveBroadcastDto;
import me.zhengjie.modules.finace_taxation.service.dto.FtLiveBroadcastQueryParam;
import me.zhengjie.modules.finace_taxation.service.mapper.FtIndustryDetailsMapper;
import me.zhengjie.modules.finace_taxation.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author 杨荣森
* @date 2021-06-23
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "live_broadcast管理")
@RequestMapping("/api/ft/ftLiveBroadcast")
public class FtLiveBroadcastController {

    private final FtLiveBroadcastService ftLiveBroadcastService;

    @Autowired
    private FtIndustryDetailsMapper ftIndustryDetailsMapper;

    @GetMapping
    @Log("查询直播")
    @ApiOperation("查询直播")
//    @PreAuthorize("@el.check('ftLiveBroadcast:list')")
    public ResponseEntity query(@RequestParam Map<String,Object> map, Pageable pageable){
        return new ResponseEntity<>(ftLiveBroadcastService.queryAll(map, pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增直播")
    @ApiOperation("新增直播")
//    @PreAuthorize("@el.check('ftLiveBroadcast:add')")
    public ResponseEntity create(@Validated @RequestBody FtLiveBroadcastDto resources){
        resources.setId("");
        return new ResponseEntity<>(ftLiveBroadcastService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改直播")
    @ApiOperation("修改直播")
//    @PreAuthorize("@el.check('ftLiveBroadcast:edit')")
    public ResponseEntity update(@Validated @RequestBody FtLiveBroadcastDto resources){
        ftLiveBroadcastService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除直播")
    @ApiOperation("删除直播")
//    @PreAuthorize("@el.check('ftLiveBroadcast:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        ftLiveBroadcastService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/getByIndustryId")
    @Log("根据行业动态查询直播地址")
    @ApiOperation("根据行业动态查询直播地址")
//    @PreAuthorize("@el.check('ftMeetings:del')")
    public ResponseEntity getByIndustryId(@RequestParam(name="industryId",required=true)  String industryId) {
        List<FtLiveBroadcast> liveList = ftLiveBroadcastService.getByIndustryId(industryId);
        return new ResponseEntity<>(liveList,HttpStatus.OK);
    }

    @Log("导出直播")
    @ApiOperation("导出直播")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, FtLiveBroadcastQueryParam query) throws IOException {
        ftLiveBroadcastService.download(ftLiveBroadcastService.queryAll(query), response);
    }

    @Log("导入直播")
    @ApiOperation("导入直播")
    @PostMapping("/import")
    public JSONObject import_order(@RequestParam("file") MultipartFile excel) throws Exception {

        JSONObject jsonObject = new JSONObject();

        Object objList = ExcelUtil.readExcel(excel, new FtLiveBroadcastImport(), 1, 1);

        if (objList == null) {
            jsonObject.put("code",500);
            jsonObject.put("msg","导入的数据不能为空");
            jsonObject.put("success",false);
            return jsonObject;
        }

        List<FtLiveBroadcastImport> ftLiveBroadcastImports = (List<FtLiveBroadcastImport>) objList;

        if (ftLiveBroadcastImports == null || ftLiveBroadcastImports.size() <= 0) {
            jsonObject.put("code",500);
            jsonObject.put("msg","导入的数据不能为空");
            jsonObject.put("success",false);
            return jsonObject;
        }
        List<FtLiveBroadcast> list = new ArrayList<>();
        for (int i = 0;i<ftLiveBroadcastImports.size();i++){
            FtLiveBroadcast ftLiveBroadcast = new FtLiveBroadcast();
            String title = ftLiveBroadcastImports.get(i).getTitle();
            FtIndustryDetails ftIndustryDetails = ftIndustryDetailsMapper.getIndustryDetailByTitle(title);
            ftLiveBroadcast.setIndustryDetailsId(ftIndustryDetails.getId());
            ftLiveBroadcast.setAddress(ftLiveBroadcastImports.get(i).getAddress());
            ftLiveBroadcast.setName(ftLiveBroadcastImports.get(i).getName());
            list.add(ftLiveBroadcast);
        }
        try {
            ftLiveBroadcastService.insertFtLiveBroadcasts(list);
            jsonObject.put("code",200);
            jsonObject.put("msg","导入成功");
            jsonObject.put("success",true);
            return jsonObject;
        }catch (Exception e){
            jsonObject.put("code",500);
            jsonObject.put("msg",e.getMessage());
            jsonObject.put("success",false);
            return jsonObject;
        }
    }

}
